package edu.sysu.service.impl;

import edu.sysu.controller.Code;
import edu.sysu.dao.BookDao;
import edu.sysu.domain.Book;
import edu.sysu.exception.BusinessException;
import edu.sysu.exception.SystemException;
import edu.sysu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        return bookDao.save(book) > 0;
    }

    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    public boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }

    public Book getById(Integer id) {
        // 在业务层模拟异常，将异常抛给表现层处理
        if (id == 1) {
            throw new BusinessException(Code.BUSINESS_ERROR, "此处不应输入此类数据");
        }

        try {
            int i = 1 / 0;
        }catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERROR, "系统繁忙，请稍后再试", e);
        }
        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }

}
